﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.Security;

public partial class StudentData_insertStudentData : System.Web.UI.Page
{
    String OWNER = "SLP2";
    String FROM_MAIL = "qooliliyahoo.com.tw@gmail.com";
    String BLANK = " ";
    String DOUBLE_TAB = "\n\n";

    protected void Page_Load(object sender, EventArgs e)
    {
        if(!IsPostBack)
        {
            Label1.Text = DateTime.Now.ToString();
            Label2.Visible = false;
        }
    }
    protected void CreateUserWizard1_CreatedUser(object sender, EventArgs e)
    {
        Roles.AddUserToRole(CreateUserWizard1.UserName, "student");

        Session["id"] = ((TextBox)CreateUserWizard1.CreateUserStep.ContentTemplateContainer.FindControl("UserName")).Text;
        Session["name"] = ((TextBox)CreateUserWizard1.CreateUserStep.ContentTemplateContainer.FindControl("TextBox1")).Text;
        Session["ssn"] = ((TextBox)CreateUserWizard1.CreateUserStep.ContentTemplateContainer.FindControl("TextBox2")).Text;
        Session["birth"] = Convert.ToDateTime(((TextBox)CreateUserWizard1.CreateUserStep.ContentTemplateContainer.FindControl("TextBox4")).Text);
        Session["email"] = ((TextBox)CreateUserWizard1.CreateUserStep.ContentTemplateContainer.FindControl("Email")).Text;
        Session["gender"] = ((TextBox)CreateUserWizard1.CreateUserStep.ContentTemplateContainer.FindControl("TextBox3")).Text;
        Session["address"] = ((TextBox)CreateUserWizard1.CreateUserStep.ContentTemplateContainer.FindControl("TextBox5")).Text;
        Session["phone"] = ((TextBox)CreateUserWizard1.CreateUserStep.ContentTemplateContainer.FindControl("TextBox6")).Text;
        Session["cellPhone"] = ((TextBox)CreateUserWizard1.CreateUserStep.ContentTemplateContainer.FindControl("TextBox7")).Text;
        Session["emergencyName"] = ((TextBox)CreateUserWizard1.CreateUserStep.ContentTemplateContainer.FindControl("TextBox9")).Text;
        Session["emergencyPhone"] = ((TextBox)CreateUserWizard1.CreateUserStep.ContentTemplateContainer.FindControl("TextBox10")).Text;
        Session["relation"] = ((TextBox)CreateUserWizard1.CreateUserStep.ContentTemplateContainer.FindControl("TextBox11")).Text;

        Session["user"] = User.Identity.Name;
        Session.Timeout = 10;

        SqlDataSource1.Insert();
        SqlDataSource6.Insert();
        SqlDataSource7.Insert();

        if(!String.IsNullOrEmpty(((TextBox)CreateUserWizard1.CreateUserStep.ContentTemplateContainer.FindControl("TextBox6")).Text))
        {
            SqlDataSource2.Insert();
        }
        if (!String.IsNullOrEmpty(((TextBox)CreateUserWizard1.CreateUserStep.ContentTemplateContainer.FindControl("TextBox7")).Text))
        {
            SqlDataSource3.Insert();
        }

        MailWebService.Mail mail = new MailWebService.Mail();

        mail.sender = FROM_MAIL;
        mail.receiver = CreateUserWizard1.Email.ToString();
        mail.subject = "學生歷程系統_帳號密碼" + BLANK + OWNER;
        mail.message = "這是您在學生歷程系 相關資訊" + BLANK + "請熟記您的帳號與密碼" + BLANK + "如遺忘請至忘記密碼查詢" + DOUBLE_TAB
                        + "您的帳號" + BLANK + CreateUserWizard1.UserName.ToString() + DOUBLE_TAB
                        + "您的密碼" + BLANK + CreateUserWizard1.Password.ToString();

        MailWebService.WebService mailWebService = new MailWebService.WebService();
        mailWebService.sendMail(mail);

        Session.Clear();
    }
    protected void TextBox2_TextChanged(object sender, EventArgs e)
    {
        ((TextBox)CreateUserWizard1.CreateUserStep.ContentTemplateContainer.FindControl("Question")).Text = "身分證號";
        ((TextBox)CreateUserWizard1.CreateUserStep.ContentTemplateContainer.FindControl("Answer")).Text =
            ((TextBox)CreateUserWizard1.CreateUserStep.ContentTemplateContainer.FindControl("TextBox2")).Text;
    }
    protected void TextBox8_TextChanged(object sender, EventArgs e)
    {
        int i = 0;
        bool find = false;

        while ((!find) && (i < GridView2.Rows.Count))
        {
            if (GridView2.Rows[i].Cells[0].Text == ((TextBox)CreateUserWizard1.CreateUserStep.ContentTemplateContainer.FindControl("TextBox8")).Text)
            {
                find = true;
            }
            i++;
        };

        if (find)
        {
            Label2.Visible = false;
            DropDownList1.SelectedValue = GridView2.Rows[i-1].Cells[1].Text;
            Session["class"] = DropDownList1.SelectedValue;
        }
        else
        {
            Label2.Visible = true;
        }
    }
}